<?php

namespace Home\Controller;

use Think\Controller;

class PhoneController extends Controller
{
    public function index()//登录后进行的操作
    {
        $logData = M('logdata');
        $res = $logData->order('id desc')->find();
        $result = $logData->order('id desc')->where(['batch' => $res['batch']])->group('project_code')->select();
        foreach ($result as &$value) {
            $value['precent'] = number_format((abs($value['diff']) / $value['ocost']) * 100, 2, '.', '');
        }
        $this->assign('batch', $res['batch']);
        $this->assign('result', $result);
        $this->display();
    }

    public function chooseBatch()//选择本年度批次
    {
        $logData = M('logdata');
        $result = $logData->where(['batch' => ['like', [date('Y').'%', date('y').'%'], 'OR']])->order('id desc')->group('batch')->select();
        foreach ($result as &$value) {
            $value['number'] = count($logData->where(['batch' => $value['batch']])->group('project_code')->select());
        }
        $this->assign('result', $result);
        $this->display();
    }

    public function batch()//显示选中批次中已有的项目
    {
        $logData = M('logdata');
        $result = $logData->where(['batch' => I('get.batch')])->order('id desc')->group('project_code')->select();
        foreach ($result as &$value) {
            $value['precent'] = number_format((abs($value['diff']) / $value['ocost']) * 100, 2, '.', '');
        }
        $this->assign('batch', I('get.batch'));
        $this->assign('result', $result);
        $this->display();
    }

    public function project($status = '', $batch = '', $project_code = '',  $type = '土建专业')
    {
        if ($status == 'baseInfo') {//查看项目的基础信息
            $batch = I('get.batch');
            $project_code = I('get.project_code');
            $baseInfo = M('base_info');
            $result = $baseInfo->where(['batch' => $batch, 'project_code' => $project_code])->find();
            $this->assign('result', $result);
            $this->display('project_base_info');
        } elseif ($status == 'detail') {//查看该项目送审信息
            $batch = I('get.batch');
            $project_code = I('get.project_code');
            $type = I('get.type') ? I('get.type') : '土建专业';
            $logData = M('logdata');
            $result = $logData->where(['batch' => $batch, 'project_code' => $project_code, 'type' => $type])->find();
            if ($resUser = M('userinfo')->where(['uid' => $result['uid']])->find()) {
                $result['company'] = $resUser['company'];
            }
            $result['precent'] = number_format((abs($result['diff']) / $result['ocost']) * 100, 2, '.', '');
            $baseInfo = M('base_info');
            $resultBase = $baseInfo->where(['batch' => $batch, 'project_code' => $project_code])->find();
            $DataView = D('DataView');
            $verdict = M('verdict');
            if ($type=="土建专业") {//由于硬编码问题，该专业与定案表中的专业填写形式不一样，所以使用条件语句处理
                $type_jx="土建";
            } elseif ($type=="铁塔专业") {
                if ($verdict->where(['project_code' => $project_code, 'profession' => ['like', '%铁塔%']])->find()) {
                    $type_jx="铁塔";
                } else {
                    $type_jx="金加工";
                }
            } elseif ($type=="交流专业") {
                $type_jx="交流";
            } elseif ($type=="配套专业") {
                $type_jx="配套";
            }
            $BasicInfos = $DataView->where(['project_code' => $project_code, 'profession' => ['like', '%'.$type_jx.'%']])->find();
            $this->assign('resultBase', $resultBase);
            $this->assign('result', $result);
            $this->assign('resStuff', $BasicInfos);
            $this->assign('batch', $batch);
            $this->assign('type', $type);
            $this->assign('project_code', $project_code);
            $this->assign('result', $result);
            $this->display('project_detail');
        }
    }

    public function moduleLook($batch = '', $project_code = '', $type = '')//查看送审项目中所使用模块的详细信息
    {
        $batch = I('get.batch');
        $project_code = I('get.project_code');
        $type = I('get.type') ? I('get.type') : '土建专业';
        $bought = M('bought');
        $result = $bought->where(['type' => $type, 'batch' => $batch,
                                      'project_code' => $project_code, ])->select();
        foreach ($result as &$value) {
            $sub_money = ((1 - floatval($value['discount']) / 100) * $value['singleprice'] + $value['safeprice']) * $value['buynumber'];
            $value['sub_money'] = number_format($sub_money, '2', '.', '');
            if ($module = M('module')->where(['number' => $value['number'], 'type' => $value['type'], 'class' => $value['class']])->find()) {
                $value['measure'] = $module['measure'];
            }
            if ($userInfo = M('userinfo')->where(['uid' => $value['uid']])->find()) {
                $value['company'] = $userInfo['company'];
            }
        }
        $baseInfo = M('base_info');
        $resultBase = $baseInfo->where(['project_code' => $project_code, 'batch' => $batch])->find();
        $this->assign('base', $resultBase);
        $this->assign('result', $result);
        $this->assign('type', $type);
        $this->assign('batch', $batch);
        $this->assign('project_code', $project_code);
        $this->display();
    }
}
